package com.dts.servlet;

import com.dts.dao.GoodDao;
import com.dts.entity.Good;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;

import java.io.IOException;
import java.util.Date;

@WebServlet(name = "add", urlPatterns = "/add")
public class AddServlet extends HttpServlet {

    public void doPost(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("name");
        String price = request.getParameter("price");
        String description = request.getParameter("description");
        try{
            Good good = new Good(0, name, Double.parseDouble(price), description,new Date());
            GoodDao.addGood(good);
            response.sendRedirect("list");
        }catch (Exception e){
            request.setAttribute("msg", "添加失败");
            request.getRequestDispatcher("add.jsp").forward(request, response);
        }
    }
}
